<?php

namespace frontend\modules\business\controllers;

use yii;
use yii\web\Controller;
use yii\helpers\Url;

/**
 * Default controller for the `business` module
 */
class MemberController extends Controller
{
    public $business = [];
    
    public $master = null;
    
    public function init()
    {
        parent::init();
        
        $this->enableCsrfValidation = false;
        
        $this->business = $this->module->params['business'];
        
        $masterConfig = $this->module->params['master'];
        
        $this->master = new yii\db\Connection([
            'dsn' => "mysql:host={$masterConfig['host']};dbname={$masterConfig['dbname']}",
            'username' => $masterConfig['username'],
            'password' => $masterConfig['password'],
            'charset' => $masterConfig['charset'],
            'tablePrefix' => $masterConfig['tablePrefix'],
        ]);
        
    }

    public function actionIndex()
    {
        return $this->render('index',['business' => $this->business]);
    }
    
    public function actionShow()
    {
        if (!Yii::$app->request->isPost) {
            $this->redirect(['default/index']);
        }

        $user_id = Yii::$app->request->post('user_id',0);
        $user_phone = Yii::$app->request->post('user_phone','');

        if (intval($user_id) == 0 && $user_phone == '') {
            $this->redirect(['default/index']);
        }
        
        $where['a.del_status'] = 0;
        
        if ($user_id) {
            $where['a.id'] = $user_id;
        }
        
        if ($user_phone) {
            $where['a.phone'] = $user_phone;
        }

        $users = (new \yii\db\Query())
        ->select(['a.id','a.sex','a.identity','a.del_status','a.type','a.is_payed','a.type_info','a.initial_amount','a.can_amount','a.sid','a.admin_id','a.auditor_id','a.age','a.is_trial','a.phone','a.s_create_time','c.username'])
        ->from('busy_member as a')
        ->leftJoin(['b' => 'busy_member_base'], 'b.id = a.id')
        ->leftJoin(['c' => 'busy_admin_user'], 'c.id = a.admin_id')
        ->where($where)
        ->all($this->master);

        return $this->render('show', ['users'=>$users]);
    }
    
    

}
